﻿Imports System.Windows.Forms
Imports ArfaUI
'Imports SwdevIUIDatabase
Public Class SettingMenuPage
    Inherits PageTemplate
    Dim arrMenuItem() As ToolStripMenuItem
    Dim roleid As String
    Private Sub SettingMenuPage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dtrole As New DataTable
        If Utils.executeSP("proc_z_getrole", New Object() {0, ""}, dtrole) Then
            If dtrole IsNot Nothing Then
                DGViewRole.DataSource = dtrole
            End If
        End If
        TreeMenu.CheckBoxes = True

        'For Each menuitem As ToolStripMenuItem In MainGeneralLedger.MenuStrip.Items
        '    Dim node As New TreeNode
        '    node.Text = menuitem.Text
        '    node.Name = "Node" & menuitem.Name
        '    If menuitem.DropDownItems.Count > 0 Then
        '        GetMenuItems(menuitem, node)
        '    End If
        '    TreeMenu.Nodes.Add(node)
        'Next
        CreateMenu()
        TreeMenu.ExpandAll()
    End Sub
    Private Sub CreateMenu()
        Dim dtmenu As New DataTable
        If Utils.executeSP("proc_z_getrolemenu", New Object() {"all", 0}, dtmenu) Then
            'If executeSP("proc_z_getmenuform", New Object() {"get"}, dsmenu) Then
            If dtmenu.Rows.Count > 0 Then
                For Each row As DataRow In dtmenu.Rows
                    If row("header") = "0" Then
                        'If row("namaform") <> "Separator" Then
                        Dim node As New TreeNode
                        node.Text = row("menuname")
                        node.Tag = row("formname")
                        node.Name = row("menuid")
                        TreeMenu.Nodes.Add(node)
                        'End If
                    Else
                        Dim nodes() As TreeNode
                        nodes = TreeMenu.Nodes.Find(row("header"), True)
                        'MessageBox.Show(nodes.Count.ToString)
                        If nodes.Count() = 1 Then
                            'If row("namaform") <> "Separator" Then
                            Dim node As New TreeNode
                            node.Text = row("menuname")
                            node.Tag = row("formname")
                            node.Name = row("menuid")
                            nodes(0).Nodes.Add(node)
                            'End If
                        End If
                    End If
                Next
            End If
        End If
        'TreeMenu.ExpandAll()
        TreeMenu.CollapseAll()
        TreeMenu.Width = 200
    End Sub

    Private Sub GetMenuItems(ByVal menu As ToolStripMenuItem, ByRef masternode As TreeNode)
        'MessageBox.Show(menu.DropDownItems.Count)
        If menu.DropDownItems.Count > 0 Then
            For Each item As ToolStripMenuItem In menu.DropDownItems
                Dim node As New TreeNode
                node.Text = item.Text
                node.Name = "Node" & item.Name
                GetMenuItems(item, node)
                masternode.Nodes.Add(node)
            Next
        End If
    End Sub
    Private Sub SetMenuItem(ByVal roleid, ByRef node)
        If node.Nodes.Count > 0 Then
            For Each childnode As TreeNode In node.Nodes
                SetMenuItem(roleid, childnode)
                If childnode.Checked = True Then
                    If Utils.exec_SP("proc_z_setrolemenu", New Object() {roleid, childnode.Name}) = False Then
                        MessageBox.Show("Simpan menu gagal.")
                    End If
                End If
            Next
        End If
    End Sub

    Private Sub UncheckNode(ByRef node)
        If node.Nodes.Count > 0 Then
            For Each childnode As TreeNode In node.Nodes
                UncheckNode(childnode)
                childnode.Checked = False
            Next
        End If
    End Sub

    Private Sub DGViewRole_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGViewRole.CellContentClick

    End Sub

    Private Sub DGViewRole_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGViewRole.CellClick
        For Each nodes As TreeNode In TreeMenu.Nodes
            If nodes.Nodes.Count > 0 Then
                UncheckNode(nodes)
            End If
            nodes.Checked = False
        Next
        Dim dtmenu As New DataTable
        If e.RowIndex = -1 Then
            Exit Sub
        End If
        roleid = DGViewRole.Rows(e.RowIndex).Cells(0).Value.ToString
        TextRole.Text = DGViewRole.Rows(e.RowIndex).Cells(1).Value.ToString
        If Utils.executeSP("proc_z_getrolemenu", New Object() {"role", roleid}, dtmenu) Then
            If dtmenu IsNot Nothing Then
                If dtmenu.Rows(0).Item(0).ToString = "NO_DATA_FOUND TO SELECT" Then
                    MessageBox.Show("NO_DATA_FOUND TO SELECT")
                Else
                    For Each row As DataRow In dtmenu.Rows
                        Dim nodes() As TreeNode
                        nodes = TreeMenu.Nodes.Find(row.Item("menuid").ToString(), True)
                        If nodes.Count = 1 Then
                            TreeMenu.SelectedNode = nodes(0)
                            TreeMenu.SelectedNode.Checked = True
                            TreeMenu.Refresh()
                        End If
                    Next
                End If

            End If
        End If
    End Sub

    Private Sub BtnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSimpan.Click
        SimpanRoleMenu()
    End Sub
    Private Sub SimpanRoleMenu()
        Me.Cursor = Cursors.WaitCursor
        'If exec_SP("proc_z_setrole", New Object() {"edit", roleid, TextRole.Text}) Then
        If Utils.exec_SP("proc_z_deleterolemenu", New Object() {roleid}) Then

        End If
        For Each nodes As TreeNode In TreeMenu.Nodes
            If nodes.Nodes.Count > 0 Then
                SetMenuItem(roleid, nodes)
            End If
            If nodes.Checked = True Then
                If Utils.exec_SP("proc_z_setrolemenu", New Object() {roleid, nodes.Name}) = False Then
                    MessageBox.Show("Simpan menu gagal.")
                    Return
                End If
            End If
        Next
        'End If
        ' End If
        Me.Cursor = Cursors.Default
        MessageBox.Show("Data sudah disimpan.")
        Dim dtrole As New DataTable
        If Utils.executeSP("proc_z_getrole", New Object() {0, ""}, dtrole) Then
            If dtrole IsNot Nothing Then
                DGViewRole.DataSource = dtrole
            End If
        End If
    End Sub
    Private Sub refreshnode(ByVal nd As TreeNode)
        For Each Tn As TreeNode In nd.Nodes

            Tn.Checked = False
        Next
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dtrole As New DataTable
        If Utils.executeSP("proc_z_getrole", New Object() {0, ""}, dtrole) Then
            If dtrole IsNot Nothing Then
                DGViewRole.DataSource = dtrole
            End If
        End If
        'For Each Tn As TreeNode In TreeMenu.Nodes

        '    Tn.Checked = False
        '    If Tn.Nodes.Count > 0 Then
        '        refreshnode(Tn)
        '    End If
        'Next
        TreeMenu.Nodes.Clear()
        TreeMenu.CheckBoxes = True
        CreateMenu()
        TreeMenu.ExpandAll()
    End Sub

    Private Sub MbGlassButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton3.Click
        Button1_Click(sender, e)
    End Sub

    Private Sub MbGlassButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton1.Click
        SimpanRoleMenu()
    End Sub

    Private Sub TreeMenu_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeMenu.AfterSelect

    End Sub

    Private Sub MbGlassButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton2.Click
        Dim DialogRole As New DialogRole
        DialogRole.ShowDialog()
        Button1_Click(sender, e)
    End Sub

    Private Sub MbGlassButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton4.Click
        Dim DialogUser As New DialogUser
        DialogUser.ShowDialog()
        'Button1_Click(sender, e)
    End Sub
End Class
